<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script>
    var userGrid;
    var userAction;
    $(function() {

        userAction = {
            add : function () {
                parent.$.modalDialog({
                    title : '添加',
                    width : 500,
                    height : 300,
                    href : '${path }/user/addPage',
                    buttons : [ {
                        text : '添加',
                        handler : function() {
                            console.log("add");
                            parent.$.modalDialog.openner_dataGrid = userGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                            var f = parent.$.modalDialog.handler.find('#userAddForm');
                            f.submit();
                        }
                    } ]
                });
            },

            edit : function () {
                var rows = $('#box').datagrid('getSelections');
                if (rows.length == 1) {
                    var index = $('#box').datagrid('getRowIndex', rows[0]);
                    console.log(rows);
                    parent.$.modalDialog({
                        title : '更新',
                        width : 500,
                        height : 400,
                        href : '${path }/user/editPage?id='+ rows[0].id,
                        buttons : [ {
                            text : '更新',
                            handler : function() {
                                parent.$.modalDialog.openner_dataGrid = userGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                                var f = parent.$.modalDialog.handler.find('#userEditForm');
                                f.submit();
                            }
                        } ]
                    });

                } else {
                    $.messager.alert('警告', '修改必须或只能选择一行！', 'warning');
                }
            },
            remove : function () {
                var rows = $('#box').datagrid('getSelections');
                if (rows.length > 0) {
                    $.messager.confirm('确定操作', '您正在要删除所选的记录吗？', function (flag) {
                        if (flag) {
                            var ids = [];
                            for (var i = 0; i < rows.length; i ++) {
                                ids.push(rows[i].id);
                            }
                            //console.log(ids.join(','));
                            $.ajax({
                                type : 'POST',
                                url : '/user/deleteUser',
                                data : {
                                    ids : ids.join(','),
                                },
                                beforeSend : function () {
                                    $('#box').datagrid('loading');
                                },
                                success : function (data) {
                                    if (data) {
                                        $('#box').datagrid('loaded');
                                        $('#box').datagrid('load');
                                        $('#box').datagrid('unselectAll');
                                        $.messager.show({
                                            title : '提示',
                                            msg : data + '个用户被删除成功！',
                                        });
                                    }
                                },
                            });
                        }
                    });
                } else {
                    $.messager.alert('提示', '请选择要删除的记录！', 'info');
                }
            },
            toenable : function () {
                var rows = $('#box').datagrid('getSelections');
                console.log(rows);

                if (rows.length > 0) {
                    $.messager.confirm('确定操作', '您正在要启用该用户吗？', function (flag) {
                        if (flag) {
                            var ids = [];
                            for (var i = 0; i < rows.length; i ++) {
                                ids.push(rows[i].id);
                            }
                            //console.log(ids.join(','));
                            $.ajax({
                                type : 'POST',
                                url : '/user/enableUser',
                                data : {
                                    ids : ids.join(','),
                                },
                                beforeSend : function () {
                                    $('#box').datagrid('loading');
                                },
                                success : function (data) {
                                    if (data) {
                                        $('#box').datagrid('loaded');
                                        $('#box').datagrid('load');
                                        $('#box').datagrid('unselectAll');
                                        $.messager.show({
                                            title : '提示',
                                            msg : '用户已被解锁成功！',
                                        });
                                    }
                                },
                            });
                        }
                    });
                } else {
                    $.messager.alert('提示', '请选择要删除的记录！', 'info');
                }
            },
            tounable : function () {
                var rows = $('#box').datagrid('getSelections');
                console.log(rows);

                if (rows.length > 0) {
                    $.messager.confirm('确定操作', '您正在要启用该用户吗？', function (flag) {
                        if (flag) {
                            var ids = [];
                            for (var i = 0; i < rows.length; i ++) {
                                ids.push(rows[i].id);
                            }
                            //console.log(ids.join(','));
                            $.ajax({
                                type : 'POST',
                                url : '/user/unableUser',
                                data : {
                                    ids : ids.join(','),
                                },
                                beforeSend : function () {
                                    $('#box').datagrid('loading');
                                },
                                success : function (data) {
                                    if (data) {
                                        $('#box').datagrid('loaded');
                                        $('#box').datagrid('load');
                                        $('#box').datagrid('unselectAll');
                                        $.messager.show({
                                            title : '提示',
                                            msg : '用户已被停用成功！',
                                        });
                                    }
                                },
                            });
                        }
                    });
                } else {
                    $.messager.alert('提示', '请选择要删除的记录！', 'info');
                }
            },
            search: function () {
                userGrid.datagrid('load', $.serializeObject($('#searchUserForm')));
            },
            repwd : function () {
                var rows = $('#box').datagrid('getSelections');
                if (rows.length == 1) {
                    var index = $('#box').datagrid('getRowIndex', rows[0]);
                    console.log(index);
                    $.ajax({
                        type : 'POST',
                        url : '/user/repwd',
                        data : {
                            id: rows[0].id},
                        beforeSend : function () {
                            $('#box').datagrid('loading');
                        },
                        success : function (data) {
                            if (data) {
                                $('#box').datagrid('loaded');
                                $('#box').datagrid('load');
                                $('#box').datagrid('unselectAll');
                                $.messager.show({
                                    title : '提示',
                                    msg : '用户已密码已经重成功！',
                                });
                            }
                        },
                    });

                } else {
                    $.messager.alert('警告', '修改必须或只能选择一行！', 'warning');
                }
            },
        }
        }

    );
    $('#userOrganizationId').combotree({
        url : '${path }/org/orgTree',
        lines : true,
        panelHeight : 'auto',
        valueField:'id',
        textField:'text',
        onChange: function (newValue, oldValue) {
            var url= '${path }/org/getdepartmentlist?id='+newValue;
            $('#userDepartment').combobox('reload',url);
        }


    });
    $('#userDepartment').combobox({
        url : '',
        panelHeight : 'auto',
        valueField:'id',
        textField:'name',

        onChange: function (newValue, oldValue) {
            var url= '${path }/position/getposlist?id='+newValue;
            console.log(url);
            $('#userPosition').combobox('reload',url);


        }

    });
    userGrid= $('#box').datagrid({
        url : '${path }/user/usersList',
        idField : 'id',
        //url : 'content.json',
        title : ' 用户列表',
        iconCls : 'icon-search',
        width:800,

        nowrap : true,
        striped: true,
        rownumbers : true,

        frozenColumns : [ [ {
            title : 'id',
            field : 'id',
            width : 40,
            hidden : true
        } ] ],
        columns : [[
            {
                field : 'loginName',
                title : '登录账号',
                sortable : true,
                width :  '10%',

            },
            {
                field : 'realName',
                title : '姓名 ',
                sortable : true,
                width :  '10%',

            },{
                field : 'orgname',
                title : '单位',
                sortable : true,
                width :  '10%',

            },
            {
                field : 'mainDepartment',
                title : '部门',
                sortable : true,
                width :  '10%',

            },
            {
                field : 'positionname',
                title : '岗位',
                sortable : true,
                width :  '10%',

            },

            {
                field : 'phone',
                title : '电话',
                sortable : true,
                width :  '10%',

            },
            {
                field : 'sex',
                title : '性别',
                sortable : true,
                width :  '10%',
                formatter : function(value, row, index) {
                    switch (value) {
                        case 0:
                            return '男';
                        case 1:
                            return '女';
                    }
                }

            },
            {
                field : 'enabled',
                title : '是否启用',
                sortable : true,
                width :  '10%',
                formatter : function(value, row, index) {
                    switch (value) {
                        case 0:
                            return '已启用';
                        case 1:
                            return '未启用';
                    }
                }

            },

        ]],
        toolbar : '#tb',
        pagination : true,
        pageSize : 10,
        pageList : [10, 20, 30],
        pageNumber : 1,

    });
    $('#orgtree').tree({
        title: 'My Panel',
        url : '${path }/org/orgTree',
        lines : true,
        collapsible:true,
        border:true,
        split:true,
        onBeforeExpand : function(node) {
            // 此处就是异步加载地所在
            if (node) {
                $(this).tree('options').url = '${path }/org/getOrgTreeDtoChildList?id=' + node.id;
            }
            return true;
        },
        onClick : function(node) {
            $('#box').datagrid('options').url = '${path }/user/selectUsersListByOrgId?id=' + node.id;
            $('#box').datagrid();
            console.log("onclick")


        }
    });

</script>
<div class="easyui-layout" data-options="fit:true">
    <div id="orgtree" data-options="region:'west',collapsed:true" style="width:180px">

    </div>
    <div data-options="region:'center'">
        <table id="box"></table>
        <div id="tb" style="padding:5px;">
        <div style="margin-bottom:5px;">
            <shiro:hasPermission name="+user+1">
                <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="userAction.add();">添加</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="+user+2">
                <a href="#" id="edit" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="userAction.edit();">修改</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="+user+8">
                <a href="#" class="easyui-linkbutton" iconCls="e-icon fa fa-circle-o-notch fa-lg " plain="true" onclick="userAction.repwd();">重置用户密码</a>
            </shiro:hasPermission>
                <shiro:hasPermission name="+user+4">
                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="userAction.remove();">删除</a>

                </shiro:hasPermission>
            <shiro:hasPermission name="+user+16">
            <a href="#" class="easyui-linkbutton" iconCls="e-icon fa fa-unlock fa-lg" plain="true" onclick="userAction.toenable();">启用</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="+user+32">
            <a href="#" class="easyui-linkbutton" iconCls="e-icon fa fa-ban fa-lg" plain="true" onclick="userAction.tounable();">停用</a>
            </shiro:hasPermission>

        </div>
            <form id="searchUserForm">
        <div style="padding:0 0 0 7px;color:#333;">
           <input name="loginname" type="text" placeholder="请输入登录名称" class="easyui-validatebox"  >
            <select id="userOrganizationId" name="userOrganizationId" placeholder="请选择单位" style="width: 140px; height: 29px;"  ></select>
            <select id="userDepartment" name="departmentid" placeholder="请选择部门" style="width: 140px; height: 29px;" class="easyui-validatebox"  ></select>
            <select id="userEditSex" name="sex" class="easyui-combobox"  data-options="width:140,height:29,editable:false,panelHeight:'auto'">

                <option value="0">男</option>
                <option value="1">女</option>
                <option value="2" selected="selected">全部</option>
            </select>
            <select id="userEditStatus" name="enabled" class="easyui-combobox"  data-options="width:140,height:29,editable:false,panelHeight:'auto'">

                <option value="0">正常</option>
                <option value="1">停用</option>
                <option value="2" selected="selected">全部</option>

            </select>
            <a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="userAction.search();">查询</a>
        </div>
            </form>
    </div>
    </div>
</div>
